package cwb1;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * This class represents a general activity of a student. It has all the common fields of an activity.
 * The activity can calculate the activity duration using the startDate and the stopDate.
 * The activity has some methods using a HashMap. It simplifies the DataEngine.
 * 
 * @author Aurora
 *
 */

public abstract class Activity implements Data, AchievementGuardian
{

	// The kind used by the database
	private static final String KIND = "Activity";
    // The initial date of the activity
	private Date startDate;
    // The end date of the activity
    private Date stopDate;
    // The ID of the user who executes the activity
    private String userId;
    // The type of the activity. Necessary for reconstructing the activity from the database by the DataEngine.
    private long activityType;
    // The ID of the activity
    private String activityId;
    // The duration of the total pause during the activity
    private long pauseDuration;
    // The initial date of the last pause during the activity
    private Date tempPauseStartDate;
    
    /**
     * Makes a new activity
     * 
     * @param startDate The start date of the activity.
     * @param userId The userId of the user who creates the activity.
     * @param activityType The type of the activity
     */
    public Activity(Date startDate, String userId, long activityType)  
    {
        this.startDate = startDate;
        this.stopDate = startDate;
        this.userId = userId;
        this.activityType = activityType;
        
        pauseDuration = 0;
        tempPauseStartDate = null;
        
        activityId = userId + startDate.getTime();
    }
    
    /**
     * Makes a new activity using the values of the map. Be careful that the keys correspond with the field names of the activity. 
     * Their is no control.
     * 
     * @param values The map with the values, having keys: "startDate" with Date, "stopDate" with Date, "userId" with String, "activityType" with long, "activityId" with String, "pauseDuration" with long.
     */
    public Activity(Map<String,Object> properties) {
    	startDate = (Date) properties.get("startDate");
    	stopDate = (Date) properties.get("stopDate");
    	userId = (String) properties.get("userId");
    	activityType = (Long) properties.get("activityType");
    	pauseDuration = (Long) properties.get("pauseDuration");
    	
    	activityId = userId + startDate.getTime();
    }
    
    /**
     * Returns the kind of the activity, used by the google database.
     * 
     * @return The kind of the activity.
     */
    public static String staticGetKind()
    {
    	return KIND;
    }
   
    /**
     * Returns the kind of the activity, used by the google database.
     * 
     * @return The kind of the activity.
     */
    public String getKind()
    {
    	return KIND;
    }
    
    /**
     * Returns the fields of the activity in a HashMap, necessary for the DataEngine.
     * 
     * @return A HashMap filled with the fields of the activity and the key as fieldname.
     */
    public HashMap<String,Object> getProperties()
    {
    	HashMap<String, Object> properties = new HashMap<String,Object>();
    	properties.put("startDate", startDate);
    	properties.put("stopDate", stopDate);
    	properties.put("userId", userId);
    	properties.put("activityType", activityType);
    	properties.put("activityId",activityId);
    	properties.put("pauseDuration", pauseDuration);
    	
    	return properties;
    }
    
    /**
     * Verifies the activity with the given activity.
     * They are the same when the id's of the activity are the same.
     * 
     * @param activity The activity to compare with.
     * @return True when they are the same. False when they are not the same.
     */
    public boolean equals(Activity activity)
    {
    	return activityId.equals(activity.getId());
    }
     
    /**
     * Returns the start date
     * 
     * @return The start date.
     */
    public Date getStartDate()
    {
        return startDate;
    }
    
    /**
     * Returns the stop date.
     * 
     * @return The stop date.
     */
    public Date getStopDate()
    {
        return stopDate;
    }
    
    /**
     * Returns the duration of the activity in thousands of a second.
     * 
     * @return The duration of the activity in thousands of a second.
     * 
     */
    public long getDuration()
    {
    	return stopDate.getTime() - startDate.getTime() - pauseDuration;
    }
    
    /**
     * Sets the user of the activity.
     * 
     * @param user The user to be set.
     */
    public void setUserId(String userId)
    {
    	this.userId = userId;
    }
    
    /**
     * Returns the id of the user who executes the activity.
     * 
     * @return The id of the user who executes the activity.
     */
    public String getUserId()
    {
        return userId;
    }
    
    /**
     * Returns the type of the activity.
     * 
     * @return The type of the activity.
     */
    public long getType()
    {
    	return activityType;
    }
    
    /**
     * This method returns the total duration of the pause during the activity.
     * 
     * @return The total duration of the pause during the activity.
     */
    public long getPauseDuration()
    {
    	return pauseDuration;
    }
    
    /**
     * This method changes the initial date of the last pause during the activity to the given value.
     * 
     * @param newTempPauseStartDate The new value for the last pause during the activity.
     */
    public void setTempPauseStartDate(Date newTempPauseStartDate)
    {
    	tempPauseStartDate = newTempPauseStartDate;
    }
    
    /**
     * This method updates the duration of the total pause and the initial date of the last pause during
     * the activity, because of giving the end date of the last pause.
     * 
     * @param TempPauseStopDate The end date of the last pause.
     */
    public void addTempPauseStopDate(Date TempPauseStopDate)
    {
    	pauseDuration = pauseDuration + TempPauseStopDate.getTime() - tempPauseStartDate.getTime();
    	tempPauseStartDate = null;
    }
    
    /**
     * Changes the type of the activity to the given value.
     * 
     * @param activityType The new value for the type of the activity.
     */
    protected void setActivityType(long activityType)
    {
    	this.activityType = activityType;
    }
    
    /**
     * Returns the id of the activity. The id is created with a combination of the userId and the startTime.
     * 
     * @return the id of the activity.
     */
    public String getId() 
    {
    	return activityId;
    }
    
    public void setStopDate(Date date) {
    	stopDate = date;
    }
    
    public void setPauseDuration(long pauseDuration) {
    	this.pauseDuration = pauseDuration;
    }
   
}
